package com.zjsru.oneDay202206;

/**
 * @Author: likew
 * @Date: 2022/7/17
 * 数组嵌套
 * 得到的结果 放入下一数组的索引
 * 输入: A = [5,4,0,3,1,6,2]
 * 输出: 4
 * 解释:
 * A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
 *
 * 其中一种最长的 S[K]:
 * S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
 *
 */
public class arrayNesting {
    public int arrayNesting(int[] nums) {
        int len = nums.length;
        int ans = 0;
        for (int i = 0; i < len; i++) {
           int cnt = 0, cur = i;
           while(nums[cur] != -1){
               cnt++;
               int c = cur;
               cur = nums[cur];
               nums[c] = -1;
           }
           ans = Math.max(cnt,ans);
        }
        return ans;
    }
    
    public static void main(String[] args) {
        arrayNesting arrayNesting = new arrayNesting();
        int[] nums = new int[]{5,4,0,3,1,6,2};
        System.out.println(arrayNesting.arrayNesting(nums));
    }
}
